package view;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jfree.data.time.TimeSeriesCollection;

import view.jfreechartTest.TimeSeriesExample;


public class ReserveAnalysisPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2553377220376390288L;
	
	private JPanel singleStatisticPanel = new JPanel(new BorderLayout());
	private JButton singleGraphBn = new JButton("绘制图像");
	private JLabel cardNoJl = new JLabel("卡号：");
	private JTextField cardNoJtf = new JTextField();

	private JPanel totalStatisticPanel = new JPanel(new BorderLayout());
	private JLabel quaryType = new JLabel("查询交易类型：");
	private JCheckBox depositButton = new JCheckBox("批量充值");
	private JCheckBox onlinePayButton = new JCheckBox("联机支付");
	private JCheckBox posPayButton = new JCheckBox("POS消费");
	private JCheckBox quaryButton = new JCheckBox("查询");

	private JButton totalGraphBn = new JButton("绘制图像");

	public ReserveAnalysisPanel() {
		setLayout(new GridLayout(6, 1));
		setBorder(BorderFactory.createTitledBorder("预付卡交易数据统计"));
		initSingleStatisticPanel();
		initTotalStatisticPanel();
		add(singleStatisticPanel);
		add(totalStatisticPanel);
	}

	// 预付卡统计图像界面
	private void initSingleStatisticPanel() {

		singleStatisticPanel
				.setBorder(BorderFactory.createTitledBorder("单卡余额"));

		singleStatisticPanel.add(cardNoJl, BorderLayout.WEST);
		singleStatisticPanel.add(cardNoJtf);
		singleStatisticPanel.add(singleGraphBn, BorderLayout.EAST);
		// panel2.add(singleStatisticPanel, BorderLayout.NORTH);

		singleGraphBn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				paintGraphForSingleCard(cardNoJtf.getText());
			}
		});
	}

	// 预付卡界面-- 全卡分析
	private void initTotalStatisticPanel() {
		totalStatisticPanel.setBorder(BorderFactory.createTitledBorder("全卡余额"));

		depositButton.setMnemonic(KeyEvent.VK_C);
		depositButton.setSelected(true);

		onlinePayButton.setMnemonic(KeyEvent.VK_G);
		onlinePayButton.setSelected(true);

		posPayButton.setMnemonic(KeyEvent.VK_H);
		posPayButton.setSelected(true);

		quaryButton.setMnemonic(KeyEvent.VK_T);
		quaryButton.setSelected(true);

		totalStatisticPanel.setLayout(new FlowLayout());
		totalStatisticPanel.add(quaryType);
		totalStatisticPanel.add(depositButton);
		totalStatisticPanel.add(onlinePayButton);
		totalStatisticPanel.add(posPayButton);
		totalStatisticPanel.add(quaryButton);
		totalStatisticPanel.add(totalGraphBn);

		totalGraphBn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				paintGraphFortotalGraph();
			}
		});
	}

	//绘制图像
	private void paintGraphForSingleCard(String cardNo) {
		// 检查cardNo是否合法

		// 然后查询数据 和 绘制图像
		String chartName = "单卡余额变化图";
		String legendName = "单卡余额";
		DrawXYFromDatabase newChart = new DrawXYFromDatabase(cardNo,chartName,legendName);
		
//		// 设置画图参数
//		TimeSeriesExample tse = new TimeSeriesExample(chartName);
//		TimeSeriesCollection dataset = new TimeSeriesCollection();
//		dataset = newChart.drawXYPlotFromResultSet(cardNo);
//
//		tse.DrawXYChart(dataset, chartName);
	}

	private void paintGraphFortotalGraph() {
		// TODO Auto-generated method stub
		String quaryCondition = "";
		int quarySelect = 1;
		if (depositButton.isSelected()) {
			quaryCondition = quaryCondition + " depositSelected ";
			quarySelect = quarySelect * 2;
		}
		if (onlinePayButton.isSelected()) {
			quaryCondition = quaryCondition + " onlinePaySelected ";
			quarySelect = quarySelect * 3;
		}
		if (posPayButton.isSelected()) {
			quaryCondition = quaryCondition + " posPaySelected ";
			quarySelect = quarySelect * 5;
		}
		if (quaryButton.isSelected()) {
			quaryCondition = quaryCondition + " quarySelected ";
			quarySelect = quarySelect * 7;
		}
		if (quaryCondition == "") {
		} else {
			DrawXYFromDatabase newChart = new DrawXYFromDatabase();
			String chartName = "整体金额变化情况";
			String legendName = "金额";
			// 设置画图参数
			TimeSeriesExample tse = new TimeSeriesExample();
			TimeSeriesCollection dataset = new TimeSeriesCollection();
			dataset = newChart.drawXYPlotFromResultSet(quarySelect,legendName);
			tse.DrawXYChart(dataset, chartName);
		}

	}
}
